// 封装音乐播放的状态
const playMode = {
    sequence: 0,
    loop: 1,
    random: 2
}

function playType(state) {
    // 拷贝一份顺利播放列表
    let newArr = [...state.sequencePlayList];
    // 如果当前切换到顺序和循环播放状态，则直接将歌曲列表给playList
    if (state.mode === 0 || state.mode === 1) {
        state.playList = [...newArr];
    }
    // 如果当前切换到随机播放状态，则打乱歌曲顺利
    else if (state.mode === 2) {
        for (let i = 0; i < newArr.length; i++) {
            const item = newArr[i];
            const ranIndex = parseInt(Math.random() * (i + 1));
            newArr[i] = newArr[ranIndex];
            newArr[ranIndex] = item;
        }
        state.playList = [...newArr]
        state.currentIndex = state.playList.findIndex(item => {
            return item.id === state.currentSong.id;
        })
    }
}

// 当前歌曲播放信息
function getCurrentSong(state) {
    if (state.currentIndex >= 0 && state.currentIndex < state.playList.length) {
        return state.playList[state.currentIndex]
    } else {
        return {}
    }
}

// 控制当前播放模式
let mode = 0;

export default {
    namespaced: true,
    state: {
        // 是否全屏播放
        fullScreen: false,
        // 当前音乐是否在播放
        playing: false,
        // 歌曲列表
        playList: [],
        // 顺利播放歌曲列表
        sequencePlayList: [],
        // 当前播放模式
        mode: playMode.sequence,
        // 当前播放歌曲列表中的下标
        currentIndex: -1,
        // 当前歌曲信息
        currentSong: {}

    },
    getters: {
        // // 当前歌曲信息
        // currentSong(state) {
        //     if (state.currentIndex >= 0 && state.currentIndex < state.playList.length) {
        //         return state.playList[state.currentIndex]
        //     }else{
        //         return {}
        //     }

        // }
    },
    mutations: {
        selectSongByIndex(state, payload) {
            state.sequencePlayList = [...payload.list]
            playType(state)
            // 歌单的原始数据要一直保留，不能被其他事件影响到歌单顺利
            state.currentIndex = payload.index
            state.currentSong = getCurrentSong(state)
            // 播放音乐
            state.playing = true
        },
        setPlaying(state, payload) {
            state.playing = payload.value
        },
        setFullScreen(state, payload) {
            state.fullScreen = payload.value
        },
        // 改变歌曲播放模式
        changeType(state) {
            mode += 1;
            state.mode = (mode % 3);
            playType(state)
            console.log(state.currentSong)
        },
        // 当点击上一首时，判断下标是否合法
        prevHandler(state) {
            if (state.currentIndex === 0) {
                state.currentIndex = state.playList.length - 1;
            } else {
                state.currentIndex -= 1;
            }
            state.currentSong = getCurrentSong(state)
        },
        // 当点击下一首时，判断下标是否合法
        nextHandler(state) {
            if (state.currentIndex === state.playList.length - 1) {
                state.currentIndex = 0;
            } else {
                state.currentIndex++;
            }
            state.currentSong = getCurrentSong(state)
        }
    },
    actions: {

    }
}